package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText rgsuser,rgsemail,rgspassword1,rgspassword2;
    private DBOpenHelper mDBOpenHelper;


    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();
        mDBOpenHelper = new DBOpenHelper(this);
    }

    private void  initView()
    {
       rgsuser  = findViewById(R.id.register_username);
       rgsemail = findViewById(R.id.register_email);
       rgspassword1 = findViewById(R.id.register_password);
       rgspassword2 = findViewById(R.id.register_enter_password);

       Button btn_register = findViewById(R.id.register_enter);
       Button btn_back = findViewById(R.id.register_back);
       btn_back.setOnClickListener(this);
       btn_register.setOnClickListener(this);

    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId()){
            case R.id.register_back://返回登录界面
                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.register_enter://注册
                //获取用户名，密码
                String username = rgsuser.getText().toString().trim();
                String email = rgsemail.getText().toString().trim();
                String password1 = rgspassword1.getText().toString().trim();
                String password2 = rgspassword2.getText().toString().trim();

                //注册验证
                if(!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password1) && !TextUtils.isEmpty(password2)) {
                    //判断两次密码是否一致
                    if (password1.equals(password2)) {
                        //将用户名密码添加到数据库中
                        mDBOpenHelper.add(username,email,password2);
                        Intent intent1 = new Intent(RegisterActivity.this, LoginActivity.class);
                        startActivity(intent1);
                        finish();
                        Toast.makeText(this, "验证通过，注册成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(this, "两次密码不一致，请重新输入", Toast.LENGTH_SHORT).show();
                    }

                }else {
                    Toast.makeText(this,"注册信息不完善，注册失败",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }


}